import requests
from utils.wifi import connect_wifi
import time


def sse_client(url):
    # 发起GET请求，设置stream=True以启用流式传输
    response = requests.get(url, stream=True)

    if response.status_code == 200:
        # 逐行读取响应数据
        while True:
            line = response.raw.readline()
            if not line:
                break  # 如果没有更多数据，则退出循环
            # 解码并打印每条消息
            decoded_line = line.decode('utf-8').strip()
            if decoded_line:
                print(decoded_line)
            time.sleep(0.1)
    else:
        print(f"Failed to establish connection: {response.status_code}")

    # 关闭连接
    # response.close()


if __name__ == "__main__":
    wlan = connect_wifi("12-303", "17762388816")
    mac = wlan.config("mac")
    sse_client(f"http://192.168.31.241:3000/sse?mac={mac}")
